#!/bin/bash
# This script whatevers mutilate
###SHELLPACK preamble mutilate-bench 1

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam --min-threads       MUTILATE_MIN_THREADS
###SHELLPACK parseargParam --max-threads       MUTILATE_MAX_THREADS
###SHELLPACK parseargParam --iterations        MUTILATE_ITERATIONS
###SHELLPACK parseargParam --duration          MUTILATE_DURATION
###SHELLPACK parseargParam --memcached-mempool MEMCACHED_MEMPOOL
###SHELLPACK parseargParam --value-size        MEMCACHED_VALUE_SIZE
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

# Start memcached server
###SHELLPACK init_only_start
$SHELLPACK_TOPLEVEL/shellpack_src/src/refresh.sh memcached
$SHELLPACK_INCLUDE/shellpack-install-memcached --mempool $MEMCACHED_MEMPOOL
if [ $? -ne 0 ]; then
	die "Failed to start memcached server"
fi

###SHELLPACK check_install_required mutilate-${VERSION}
###SHELLPACK init_only_end

# Restart server
$SHELLPACK_INCLUDE/shellpack-install-memcached --shutdown
if [ "$MEMCACHED_MEMPOOL" = "" ]; then
	die "Must specify memcached memory pool size"
fi
$SHELLPACK_INCLUDE/shellpack-install-memcached --mempool $MEMCACHED_MEMPOOL

echo Warming run for $((MUTILATE_DURATION*2)) seconds
MUTILATE_ITEMS=$(($MEMCACHED_MEMPOOL/$MUTILATE_VALUE_SIZE))
cd $SHELLPACK_SOURCES/mutilate-${VERSION}-installed || die Failed to cd to mutilate directory
./bin/mutilate \
	-s 127.0.0.1 \
	-T $MUTILATE_MAX_THREADS \
	-V $MUTILATE_VALUE_SIZE \
	-r $MUTILATE_ITEMS \
	-t $((MUTILATE_DURATION*2))

# Start the test
###SHELLPACK threads_large_stride_begin $MUTILATE_MIN_THREADS $MUTILATE_MAX_THREADS
monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS

###SHELLPACK iteration_begin $MUTILATE_ITERATIONS
	echo Starting mutilate client $MUTILATE_CONCURRENCY
	echo o $MEMCACHED_MEMPOOL memcached pool
	echo o $NR_THREADS concurrent requests
	echo o $MUTILATE_VALUE_SIZE value size
	echo o $MUTILATE_DURATION second duration
	echo o $MUTILATE_ITEMS items per iteration

	echo Iteration:$ITERATION
	./bin/mutilate \
		-s 127.0.0.1 \
		-T $NR_THREADS \
		-V $MUTILATE_VALUE_SIZE \
		-r $MUTILATE_ITEMS \
		-t $MUTILATE_DURATION \
		    | tee -a $LOGDIR_RESULTS/mutilate-$NR_THREADS-$ITERATION.log
###SHELLPACK iteration_end

monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
###SHELLPACK threads_stride_end

$SHELLPACK_INCLUDE/shellpack-install-memcached --shutdown

exit $SHELLPACK_SUCCESS
